package controller;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import po.User;
import service.UserService;
import utils.ServerResponse;
import utils.UuidUtils;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/26 14:17
 * @description:
 */

@Controller
@RequestMapping("/manage/user")
public class UserController {

  @Autowired
  private UserService userService;

  @RequestMapping("/findAllUser")
  @ResponseBody
  public ServerResponse getAllUser() {
    User user = new User();
    user.setId(1);
    user.setUsername("hhh");
    user.setPassword("pishf");
    user.setEmail("aa@mail.com");
    user.setQuestion("hello");
    user.setAnswer("word");
    user.setRole(0);
    user.setCreate_time(new Date());
    user.setUpdate_time(new Date());

    return ServerResponse.createSuccess(user);
  }

//登录
  @RequestMapping(method = RequestMethod.POST,value = "/login")
  @ResponseBody
  public ServerResponse login(
      @RequestParam(value = "username",required = false) String username,
      @RequestParam(value = "password",required = false) String password,
      HttpSession session) {
    // userService
    // if success -> ServerResponse<User>
    // if error   -> ServerResponse.message = 密码错误
    ServerResponse<User> response = userService.login(username, password);
    if (response.success()) {
      // save to session
      session.setAttribute("user", response.getData());
    }

    return response;
  }

  @RequestMapping("/list")
  @ResponseBody
  public ServerResponse findAll(HttpSession session)
  {
    ServerResponse<User> list=userService.findAll();
    if(list.success())
    {
      session.setAttribute("data",list.getData());
    }
    return list;
  }

  //注册
  @RequestMapping(method = RequestMethod.POST,value = "/register")
  @ResponseBody
  public ServerResponse register(User user) throws Exception
  {
  ServerResponse userResponse= userService.insert(user);
 return userResponse;
  }
  //获取登陆信息，并强制登录
  @RequestMapping(method = RequestMethod.GET,value = "/get_user_info")
  @ResponseBody
  public ServerResponse register(HttpSession session,@RequestParam(value = "username",required = false) String username,@RequestParam(value = "password",required = false) String password)
  {

    ServerResponse<User> userResponse= userService.check(username,password);
    if(session==null)
    {
      userResponse.setStatus(1);
        userResponse.setMsg("用户未登录,无法获取当前用户信息");
    }
    if(userResponse.success())
    {
      session.setAttribute("date",userResponse.getData());
    }
    return userResponse;
  }
  //忘记问题
  @RequestMapping(method = RequestMethod.POST,value = "/forget_get_question")
  @ResponseBody
  public ServerResponse forGet(@RequestParam(value = "username",required = false)String username,HttpSession httpSession) {
      ServerResponse<User> userResponse = userService.findByUsername(username);
      if (userResponse.success()) {
          httpSession.setAttribute("date", userResponse.getData());
      }

      return userResponse;
  }
  //退出登录
  @RequestMapping(method = RequestMethod.GET,value = "/logout.do")
    @ResponseBody
  public ServerResponse logout(HttpSession session)
  {
      ServerResponse serverResponse=userService.removeSession(session);
      return serverResponse;
  }
//获取登录信息
  @RequestMapping(method = RequestMethod.GET,value = "/get_information")
    @ResponseBody
    public ServerResponse get_information(HttpSession session)
    {
        ServerResponse serverResponse=userService.get_information(session);
        if(serverResponse.success())
        {
            session.setAttribute("data",serverResponse.getData());
        }
        return serverResponse;
    }
    //重置密码
    @RequestMapping(method = RequestMethod.GET,value = "/reset_password")
    @ResponseBody
    public ServerResponse reset_password(@RequestParam(value = "passwordOld",required = false) String passwordOld,@RequestParam(value = "passwordNew",required = false) String passwordNew,HttpSession session)
    {
        ServerResponse serverResponse=userService.reset_password(passwordOld,passwordNew,session);
        return serverResponse;
    }
    //登录状态更新个人信息
    @RequestMapping(method = RequestMethod.GET,value = "/update_information")
    @ResponseBody
    public ServerResponse update_information(@RequestParam(value = "username",required = false) String username,@RequestParam(value = "email",required = false) String email,@RequestParam(value = "phone",required = false) String phone,@RequestParam(value = "question",required = false) String question,@RequestParam(value = "answer",required = false) String answer,HttpSession session)
    {
        ServerResponse serverResponse=userService.update_information(username,email,phone,question,answer,session);
        return serverResponse;
    }

    //提交问题答案
    @RequestMapping(method = RequestMethod.GET,value = "/forget_check_answer")
    @ResponseBody
    public ServerResponse forget_check_answer(@RequestParam(value = "username",required = false)String username,@RequestParam(value = "question",required = false)String question ,@RequestParam(value = "answer")String answer,HttpSession session)
    {
        ServerResponse serverResponse=userService.forget_check_answer(username,question,answer);
        if(serverResponse.success()) {
            UuidUtils uuidUtils=new UuidUtils();
            String taken = uuidUtils.getUuid();
            serverResponse.setData(taken);
            session.setAttribute("date",serverResponse.getData());
        }
        return serverResponse;
    }
    //忘记密码的重设密码
  @RequestMapping(method = RequestMethod.GET,value = "/forget_reset_password.do")
  @ResponseBody
  public ServerResponse forget_reset_password(@RequestParam(value = "username",required = false)String username,@RequestParam(value = "passwordNew",required = false)String passwordNew,@RequestParam(value = "forgetToken",required = false) String forgetToken,HttpSession session)
  {
    ServerResponse<User> serverResponse=userService.forget_reset_password(username,passwordNew,forgetToken,session);

    return serverResponse;
  }

}
